import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { Input } from '@angular/core';
import { HeroService } from '../hero.service';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';

@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {

  //hero需要点一个@Input属性，因为外部的HeroesComponent组件将会绑定它。
  //因为他的值是使用hero-detail的父组件传递进来的
  @Input() hero : Hero;

  constructor(private route : ActivatedRoute, 
    private heroService : HeroService, 
    private location : Location) 
    {}

  ngOnInit(): void {
    this.getHero();
  }

  getHero() : void {
    const id = +this.route.snapshot.paramMap.get('id');
    this.heroService.getHero(id).subscribe(hero => this.hero = hero);
  }

  goBack() : void {
    this.location.back();
  }

  save() : void {
    this.heroService.updateHero(this.hero).subscribe(()=>this.goBack);
  }

}
